﻿@model ApplicationUserViewModel

<form asp-area="Admin" asp-controller="User" asp-action="Edit" method="post" class="form-horizontal">
    <hr />
    <div asp-validation-summary="All" class="text-danger"></div>
    <input type="hidden" asp-for="Id" />
    <div class="form-group">
        <label asp-for="Name" class="col-md-2 control-label"></label>
        <div class="col-md-4">
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="PhoneNumber" class="col-md-2 control-label"></label>
        <div class="col-md-4">
            <input asp-for="PhoneNumber" class="form-control" />
            <span asp-validation-for="PhoneNumber" class="text-danger"></span>
        </div>
    </div>
    @*<div class="form-group">
        <label asp-for="Email" class="col-md-2 control-label"></label>
        <div class="col-md-4">
            <input asp-for="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
    </div>*@

    @{
        var roles = "";
        foreach (var item in Model.RoleNames)
        {
            roles += item + " ";
        }
    }
    <div class="form-group">
        <label asp-for="RoleNames" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input id="RoleList" value="@roles" class="form-control" style="display:inline;width:300px;" onclick="RoleList_Clicked()" />
            <div style="display:inline;">
                <select id="RoleName" name="RoleName" asp-items="ViewBag.RoleList" class="form-control" style="display:inline;width:300px;"></select>
            </div>
            <div style="padding-left:15px;display:inline;">
                <input id="btnAddRole" type="button" value="添加角色" class="btn btn-default" onclick="AddRole_Clicked()" />
            </div>
            <div style="padding-left:15px;display:inline;">
                <input id="btnDeleteRole" type="button" value="删除角色" class="btn btn-default" onclick="DeleteRole_Clicked()" />
            </div>
        </div>
    </div>

    <div class="form-group">
        <label asp-for="DepartmentId" class="col-md-2 control-label"></label>
        <div class="col-md-4">
            <select asp-for="DepartmentId" asp-items="ViewBag.DepartmentList" class="form-control">
                <option value="0">请选择</option>
            </select>
            <span asp-validation-for="DepartmentId" class="text-danger"></span>
        </div>
    </div>
   
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <button type="submit" class="btn btn-default col-md-2">确定</button>
        </div>
    </div>
</form>


@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }

    <script type="text/javascript">
        //使RoleList永远失去焦点，达到只读的目的
        function RoleList_Clicked() {
            $("#RoleList").blur();
        }
        function AddRole_Clicked() {
            $.ajax({
                type: "POST",
                url: "/admin/user/useraddrole",
                data: "userId=" + $("#Id").val() + "&roleName=" + $("#RoleName option:selected").text(),
                success: function (msg) {
                    var str = $("#RoleList").val() + msg;
                    $("#RoleList").val(str);

                }
            })
        }
        function DeleteRole_Clicked() {
            $.ajax({
                type: "POST",
                url: "/admin/user/userdeleterole",
                data: "userId=" + $("#Id").val() + "&roleName=" + $("#RoleName option:selected").text(),
                success: function (msg) {
                    var str = $("#RoleList").val().replace(msg, "");
                    $("#RoleList").val(str);

                }
            })
        }
        
    </script>
}
